package com.yunbiao.ybbusstopplate.base.alive

import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import android.text.TextUtils
import android.util.Log
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ServiceUtils
import com.yunbiao.ybbusstopplate.utils.CommonUtils
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit

class KeepAliveService : Service(){
    private val TAG = "KeepAliveService"


    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.d(TAG, "onStartCommand: ")
        startAliveing()
        return super.onStartCommand(intent, flags, startId)
    }

//    class AliveingRunnable : Runnable{
//
//        private var context:Context?=null
//        private var packageName:String=""
//
//        constructor(context: Context,packageName:String){
//            this.context=context
//            this.packageName=packageName
//        }
//
//        override fun run() {
//            if (TextUtils.isEmpty(packageName)){
//                return
//            }
//
//            val appRunning = CommonUtils.getInstance().isAppRunning(context, packageName, true)
//            Log.d("KeepAliveService", "run: -----APP保活检测------$appRunning");
//            // 检测应用是否在最上层，没有则重新打开至上层
//            if (!appRunning) {
//                LogUtils.dTag("KeepAliveService","startActivity")
//                CommonUtils.getInstance().lunchAppWithPackageName(context, packageName)
//            }
//        }
//    }

    lateinit var subscribe:Disposable;
    private fun startAliveing(){
        subscribe = Observable
            .interval(1, 30, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer {
                val appRunning = CommonUtils.getInstance().isAppRunning(this, packageName, true)
                Log.d("KeepAliveService", "run: -----APP保活检测------$appRunning");
                // 检测应用是否在最上层，没有则重新打开至上层
                if (!appRunning) {
                    LogUtils.dTag("KeepAliveService", "startActivity")
                    CommonUtils.getInstance().lunchAppWithPackageName(this, packageName)
                }
            });
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy: ")
        if(!subscribe.isDisposed){
            Log.d(TAG, "onDestroy: 1111111111111111")
            subscribe.dispose()
        }
    }

    override fun onBind(p0: Intent?): IBinder? {
        return null
    }

    companion object{
        fun startKeepAliveService(context: Context){
            if (ServiceUtils.isServiceRunning(KeepAliveService().javaClass)){
                return
            }
            context.startService(Intent(context,KeepAliveService::class.java))
        }

        fun stopKeepAliveService(context: Context){
            val serviceRunning = ServiceUtils.isServiceRunning(KeepAliveService().javaClass)
            Log.d("KeepAliveService", "stopKeepAliveService: 1111111111111111$serviceRunning")

            if (serviceRunning){
                context.stopService(Intent(context,KeepAliveService::class.java))
            }
        }
    }
}